var failMessage = [];
var easeChat;
function startServey() {
    //1秒监测一次消息未读情况
    easeChat = api.require('easeChat');
    setInterval(function () {
        var removeItem = [];
        for (var i = 0; i < failMessage.length; i++) {
            var item = failMessage[i];
            easeChat.loadMessageWithId({
                conversationId: item.conversationId,
                type: item.type,
                messageId: item.messageId
            }, function (ret, err) {
                if (JSON.stringify(ret.message) === "{}") {
                    //监测到信息发送成功了
                    searchMsg(item.conversationId, item.displayName, '');
                    removeItem.push(item);
                    //移除消息
                    for (var i = 0; i < removeItem.length; i++) {
                        var index = 0;
                        for (var j = 0; j < failMessage.length; j++) {
                            if (removeItem[i].messageId === failMessage[j].messageId) {
                                index = j;
                                break;
                            }
                        }
                        failMessage.splice(index, 1);
                    }
                }
            });
        }
    }, 1000);
}

function searchMsg(conversationId, displayName, msgId) {
    easeChat.loadMessageFromDB({
        conversationId: conversationId,
        type: 'groupChat',
        pagesize: 100,
        startMsgId: msgId
    }, function (ret) {

        for (var i = 0; i < ret.messages.length; i++) {
            if (ret.messages[i].body.displayName === displayName && ret.messages[i].body.remotePath.length > 0) {
                uploadMsgToCFnet(ret.messages[i]);
                return;
            }
        }
        if (ret.messages.length < 100)
            return;
        searchMsg(conversationId, displayName, ret.messages[0].messageId);
    });
}

//上传消息至长风网
function uploadMsgToCFnet(msg) {
    console.log("未发送的消息:" + "发送成功");
    cfnetppPOST(url_ring_updateMsg, {
        msg: JSON.stringify(msg)
    }, false, function (data, err) {
    })
}